package cn.buyclss.backend.async;


import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;

import javax.mail.internet.MimeMessage;

public class MailTask implements Runnable {

    private String code;
    private String email;
    private JavaMailSender javaMailSender;
    //发送邮件的邮箱，要与df.properties中的一致
    public static final String MAIL_FROM = "1619110231@qq.com";

    public MailTask(String code, String email, JavaMailSender javaMailSender) {
        this.code = code;
        this.email = email;
        this.javaMailSender = javaMailSender;
    }

    @Override
    public void run() {
        javaMailSender.send(new MimeMessagePreparator() {
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {
                System.out.println("开始发邮件...");
                MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
                mimeMessageHelper.setFrom(MAIL_FROM);
                mimeMessageHelper.setTo(email);
                mimeMessageHelper.setSubject("一封验证码邮件");
                StringBuilder sb = new StringBuilder();
                sb.append("<html><head></head><body>");

                sb.append("验证码是：<b>b2bc</b> </body>");

                mimeMessageHelper.setText(sb.toString(), true);

                System.out.println("结束发邮件...");
            }
        });
    }
}








